from app import db
from datetime import datetime

class Shop(db.Model):
    __tablename__ = 'shop'

    shop_id = db.Column(db.String(36), primary_key=True, comment='店铺ID')
    shop_name = db.Column(db.String(50), nullable=False, comment='店铺名称')
    shop_desc = db.Column(db.String(500), comment='店铺描述')
    shop_logo = db.Column(db.String(200), comment='店铺logo')
    shop_status = db.Column(db.String(1), default='0', comment='店铺状态（0正常 1关闭）')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    remark = db.Column(db.String(500), comment='备注')

    owner_id = db.Column(db.String(36), db.ForeignKey('sys_user.ID'))

    def to_dict(self):
        json = {
            'shopId': self.shop_id,
            'shopName': self.shop_name,
            'shopDesc': self.shop_desc,
            'shopLogo': self.shop_logo,
            'shopStatus': self.shop_status,
            'ownerId': self.owner_id,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'updateTime': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
            'remark': self.remark
        }
        if self.user:
            json['owner_name'] = self.user.LOGINNAME

        return json